package com.cehome.cloudbox.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cehome.cloudbox.base.R;
import com.cehome.cloudbox.doman.dto.*;
import com.cehome.cloudbox.doman.vo.ProcInsPageQueryVo;
import com.cehome.cloudbox.doman.vo.ProcInsPageShowVo;
import com.cehome.cloudbox.doman.vo.ProcMyProcPageShowVo;
import com.cehome.cloudbox.doman.vo.ProcStateVo;
import com.cehome.cloudbox.flowalbe.FlowableTaskService;
import com.cehome.cloudbox.service.IProcInstanceService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * <p>
 * 流程实例接口 控制器
 * </p>
 */
@Api(value = "流程实例操作API", tags = "流程实例接口")
@RestController
@RequestMapping("/flowable/instance")
public class ProcInstanceController {

    @Resource
    private IProcInstanceService iProcInstanceService;

    @Resource

    FlowableTaskService flowableTaskService;

//    @ApiOperation(value = "开始")
//    @PostMapping(value = "/start")
//    public R<ProcStateDto> start(@RequestBody ProcStartDto param) {
//        return R.data(iProcInstanceService.start(param));
//    }

    @ApiOperation(value = "开始")
    @PostMapping(value = "/start")
    public R<ProcStateDto> flowableInstanceStart(@RequestBody ProcStartDto param) {
        return R.data(iProcInstanceService.start(param));
    }


    @ApiOperation(value = "撤回")
    @PostMapping(value = "/revoke")
    public R<ProcStateDto> revoke(@RequestBody ProcInsHandleDto param) {
        return R.data(iProcInstanceService.revoke(param));
    }

    @ApiOperation(value = "终止")
    @PostMapping(value = "/termination")
    public R<ProcStateDto> termination(@RequestBody ProcInsHandleDto param) {
        return R.data(iProcInstanceService.termination(param));
    }

    @ApiOperation(value = "取消")
    @PostMapping(value = "/cancel")
    public R<ProcStateDto> cancel(@RequestBody ProcInsHandleDto param) {
        return R.data(iProcInstanceService.cancel(param));
    }



    @ApiOperation(value = "根据流程实例ID查询流程审批历史")
    @GetMapping(value = "/approvalHistory/{procInsId}")
    public R approvalHistory(@PathVariable("procInsId") String procInsId) throws Exception{
        System.out.println(new ObjectMapper().writeValueAsString(iProcInstanceService.approvalHistory(procInsId)));
        return R.data(iProcInstanceService.approvalHistory(procInsId));
    }

    @GetMapping(value = "/processImg/{processInstanceId}")
    @ApiOperation(httpMethod = "GET", produces = "application/json",value = "获取流程实例图片",notes = "获取流程实例图片")
    public void getProcessInstanceImg(@ApiParam(value = "流程实例id",required = true)@PathVariable String processInstanceId, HttpServletResponse response) throws Exception {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            response.setContentType("image/jpeg");
            inputStream = flowableTaskService.getProccessActivitiPathImage(processInstanceId);
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream, outputStream);
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }

    @ApiOperation(value = "根据流程实例ID查询流程状态")
    @GetMapping(value = "/procIncState/{procInsId}")
    public R<ProcStateVo> procIncState(@PathVariable("procInsId") String procInsId) {
        return R.data(iProcInstanceService.stateAndNextTaskId(procInsId));
    }

    @ApiOperation(value = "查询流程实例列表")
    @GetMapping(value = "/page")
    public R<Page<ProcInsPageShowVo>> page(ProcInsPageQueryVo queryVo) {
        return R.data(iProcInstanceService.page(queryVo));
    }

    @ApiOperation(value = "我的流程实例列表")
    @GetMapping(value = "/myProc")
    public R<Page<ProcMyProcPageShowVo>> myProc(ProcInsPageQueryVo queryVo) {
        return R.data(iProcInstanceService.myProcess(queryVo));
    }

}
